class PushJob < ApplicationJob
  queue_as :default

  def perform(device_type, device_token, message, extras={})
    if device_type.eql?("ios")
      notification = Houston::Notification.new(device: device_token)
      notification.alert = {"title": "车辆预警", "body": message}.merge(extras)
      notification.badge = 1
      notification.sound = "123.wav"
      APN.push(notification)
    else
      conn = Faraday.new(url: $jpush_host)
      conn.headers['Authorization'] = " Basic #{Base64.strict_encode64("5ed13f38a42d4012588b64b0:00ddf06b3959bab3156c945e")}"
      conn.post "/v3/push", {
          platform: "android",
          audience: {
              registration_id: [device_token]
          },
          notification: {
              android: {
                  alert: message,
                  title: "车辆预警",
                  builder_id: 1,
                  extras: extras
              }
          }
      }.to_json
    end
  end
end
